﻿using GBT.SystemBusinessCore;
using GBT.ParamEntity;
using GBT.SystemEnties;
using GBT.Tool.Core;
using Newtonsoft.Json.Linq;
using NLog;
using Persistence.Core;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using GBT.ManageEnties;

namespace QueryDataLogic.Core
{
    public class BasicInfo
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        ConfigManage cm = new ConfigManage();
        QueryData qd = new QueryData();
        DataPaging dp = new DataPaging();
        TimeManagement tm = new TimeManagement();

        /// <summary>
        /// 根据区域ID获取数据库信息
        /// </summary>
        /// <param name="districtID"></param>
        /// <returns></returns>
        public string GetDBName(int districtID)
        {
            string dbName = "";
            try
            {
                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select c.DatabaseName from District d inner join Company c on d.companyID = c.ID " +
                                "where d.districtID = @districtID  "
                };

                comm.Parameters.AddWithValue("@districtID", districtID);
                JArray ja = qd.GetData(comm, "MealManage");

                if (ja.Count > 0)
                {
                    dbName = (string)ja[0]["DatabaseName"];
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
            }
            return dbName;
        }

        /// <summary>
        /// 根据公司ID获取数据库信息
        /// </summary>
        /// <param name="districtID"></param>
        /// <returns></returns>
        public string GetDBNameToCompany(int companyID)
        {
            string dbName = "";
            try
            {
                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "SELECT DatabaseName FROM Company WHERE ID = @ID "
                };

                comm.Parameters.AddWithValue("@ID", companyID);
                JArray ja = qd.GetData(comm, "MealManage");

                if (ja.Count > 0)
                {
                    dbName = (string)ja[0]["DatabaseName"];
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
            }
            return dbName;
        }

        /// <summary>
        /// 根据邀请码获取数据库信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public string GetDBNameToCode(string code)
        {
            string dbName = "";
            try
            {
                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select ic.code,c.DatabaseName from InvitationCode ic " +
                        "inner join District d on ic.DistrictID = d.DistrictID " +
                        "inner join Company c on d.CompanyID = c.ID " +
                        "where ic.code = @code "
                };

                comm.Parameters.AddWithValue("@ID", code);
                JArray ja = qd.GetData(comm, "MealManage");

                if (ja.Count > 0)
                {
                    dbName = (string)ja[0]["DatabaseName"];
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
            }
            return dbName;
        }

        /// <summary>
        /// 根据区域ID获取公司ID
        /// </summary>
        /// <param name="districtID"></param>
        /// <returns></returns>
        public int GetCompanyID(int districtID)
        {
            int companyID = 0;
            try
            {
                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select d.companyID from District d where d.districtID = @districtID  "
                };

                comm.Parameters.AddWithValue("@districtID", districtID);
                JArray ja = qd.GetData(comm, "MealManage");

                if (ja.Count > 0)
                {
                    companyID = (int)ja[0]["companyID"];
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
            }
            return companyID;
        }

        /// <summary>
        /// 获取区域信息
        /// </summary>
        /// <returns></returns>
        public JObject GetDistrictInfo(int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                int companyID = GetCompanyID(districtID);

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select DistrictID as id,DistrictNumber as number,DistrictName as name,companyID from District " +
                    "where CompanyID = @companyID "
                };
                comm.Parameters.AddWithValue("@companyID", companyID);

                JArray ja = qd.GetData(comm, "MealManage");

                if (ja.Count > 0)
                {
                    JArray jaDistrictTitleArray = new JArray();
                    JArray jaDistrictArray = new JArray();
                    JObject joDistrictItem = new JObject();
                    for (int districtIndex = 0; districtIndex < ja.Count; districtIndex++)
                    {
                        jaDistrictTitleArray.Add((string)ja[districtIndex]["name"]);
                        joDistrictItem = new JObject();
                        joDistrictItem = (JObject)ja[districtIndex];
                        jaDistrictArray.Add(joDistrictItem);
                    }

                    joResult.Add("districtTitleArray", jaDistrictTitleArray);
                    joResult.Add("districtArray", jaDistrictArray);
                    joResult.Add("message", "区域信息加载成功。");
                    joResult.Add("state", true);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "区域信息加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        public Manage_District GetDistrictInfo(Manage_District district)
        {
            GBT.ManageBusinessCore.DbContext<GBT.ManageEnties.District> dbContext = new GBT.ManageBusinessCore.DbContext<GBT.ManageEnties.District>();
            var districtEntity = dbContext.GetList(t => t.DistrictID == district.DistrictID)?
                .Select(t => new Manage_District
                {
                    DistrictID = t.DistrictID,
                    DistrictNumber = t.DistrictNumber,
                    DistrictName = t.DistrictName,
                    CreationTime = t.CreationDate
                })
                .FirstOrDefault();
            return districtEntity;
        }

        /// <summary>
        /// 获取餐点信息
        /// </summary>
        /// <returns></returns>
        public JObject GetMeal(int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select mealId AS id,mealNumber AS number,mealName AS name,price,familyPrice,districtID,startTime,endTime from mealTime " +
                    "where districtID = @districtID order by mealId "
                };

                comm.Parameters.AddWithValue("@districtID", districtID);

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);

                if (ja.Count > 0)
                {
                    JArray jaMealTitle = new JArray();
                    JArray jaMealTitleItem = new JArray();
                    JArray jaMeal = new JArray();
                    JArray jaMealItem = new JArray();
                    JObject joMealItem = new JObject();
                    JObject joEmpty;
                    for (int mealIndex = 0; mealIndex < ja.Count; mealIndex++)
                    {
                        jaMealTitleItem = new JArray();
                        jaMealTitleItem.Add("");
                        jaMealTitleItem.Add((string)ja[mealIndex]["name"]);
                        jaMealTitle.Add(jaMealTitleItem);

                        jaMealItem = new JArray();
                        joMealItem = new JObject();
                        joEmpty = new JObject();
                        joEmpty["id"] = 0;
                        joEmpty["mealId"] = ja[mealIndex]["id"];
                        joEmpty["number"] = ja[mealIndex]["number"];
                        joEmpty["name"] = ja[mealIndex]["name"];
                        joEmpty["qty"] = 0;
                        joEmpty["price"] = ja[mealIndex]["price"];
                        joEmpty["districtID"] = ja[mealIndex]["districtID"];
                        joEmpty["startTime"] = ja[mealIndex]["startTime"];
                        joEmpty["endTime"] = ja[mealIndex]["endTime"];

                        joMealItem["id"] = 1;
                        joMealItem["mealId"] = ja[mealIndex]["id"];
                        joMealItem["number"] = ja[mealIndex]["number"];
                        joMealItem["name"] = ja[mealIndex]["name"];
                        joMealItem["qty"] = 1;
                        joMealItem["price"] = ja[mealIndex]["price"];
                        joMealItem["districtID"] = ja[mealIndex]["districtID"];
                        joMealItem["startTime"] = ja[mealIndex]["startTime"];
                        joMealItem["endTime"] = ja[mealIndex]["endTime"];
                        jaMealItem.Add(joEmpty);
                        jaMealItem.Add(joMealItem);
                        jaMeal.Add(jaMealItem);
                    }

                    //logger.Info(jaMeal);
                    joResult.Add("mealTitleArray", jaMealTitle);
                    joResult.Add("mealObjectArray", jaMeal);
                    joResult.Add("message", "餐点信息加载成功。");
                    joResult.Add("state", true);
                }
                else
                {
                    joResult.Add("data", "");
                    joResult.Add("message", "餐点信息加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", "");
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取餐点信息(可选参数：餐点ID，区域ID，餐点编码，餐点名称)
        /// </summary>
        /// <param name="mealTime"></param>
        /// <param name="totalNumber"></param>
        /// <param name="totalPage"></param>
        /// <returns></returns>
        public List<System_MealTime> GetMealTimeInfo(System_MealTime mealTime, ref int totalNumber, ref int totalPage)
        {
            string dbName = GetDBName((int)mealTime.DistrictID);
            Manage_District mDistrict = new Manage_District();
            mDistrict.DistrictID = mealTime.DistrictID;
            var district = GetDistrictInfo(mDistrict);
            GBT.SystemBusinessCore.DbContext<MealTime> dbContext = new GBT.SystemBusinessCore.DbContext<MealTime>(dbName);
            var db = dbContext.Db;
            var mealTimeInfoList = db.Queryable<MealTime>()
                .WhereIF(mealTime.MealId != null, t => t.MealId == mealTime.MealId)
                .WhereIF(mealTime.DistrictID != null, t => t.DistrictID == mealTime.DistrictID)
                .WhereIF(!string.IsNullOrEmpty(mealTime.MealNumber), t => t.MealNumber.Contains(mealTime.MealNumber))
                .WhereIF(!string.IsNullOrEmpty(mealTime.MealName), t => t.MealName.Contains(mealTime.MealName))
                .Select(t => new System_MealTime
                {
                    MealId = t.MealId,
                    MealNumber = t.MealNumber,
                    MealName = t.MealName,
                    Price = t.Price,
                    FamilyPrice = t.FamilyPrice,
                    startTime = t.startTime,
                    endTime = t.endTime,
                    DistrictID = t.DistrictID,
                    CreationDate = t.CreationDate
                }).ToPageList(mealTime.PageIndex, mealTime.PageSize, ref totalNumber, ref totalPage);

            var diningHallInfoListNew = mealTimeInfoList
                .Select(t => new System_MealTime
                {
                    MealId = t.MealId,
                    MealNumber = t.MealNumber,
                    MealName = t.MealName,
                    Price = t.Price,
                    FamilyPrice = t.FamilyPrice,
                    startTime = t.startTime,
                    endTime = t.endTime,
                    DistrictID = t.DistrictID,
                    DistrictNumber = district.DistrictNumber,
                    DistrictName = district.DistrictName,
                    CreationDate = t.CreationDate
                }).ToList();

            return diningHallInfoListNew;
        }

        /// <summary>
        /// 获取餐点信息 列表（单选）
        /// </summary>
        /// <returns></returns>
        public JObject GetMealList(int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select mealId AS id,mealNumber AS number,mealName AS name,price,familyPrice,districtID,startTime,endTime from mealTime " +
                    "where districtID = @districtID order by mealId"
                };
                comm.Parameters.AddWithValue("@districtID", districtID);

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);

                if (ja.Count > 0)
                {
                    //logger.Info(ja);
                    joResult.Add("data", ja);
                    joResult.Add("message", "餐点信息加载成功。");
                    joResult.Add("state", true);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "餐点信息加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取食堂信息
        /// </summary>
        /// <returns></returns>
        public JObject GetDiningHall(int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select h.hallid AS id,h.hallNumber AS number,h.hallName as name,h.districtID,h.deadline,h.IsEnable,h.CreationDate as creationTime from dininghall h " +
                    "where h.districtID = @districtID and h.IsEnable = 1 "
                };
                comm.Parameters.AddWithValue("@districtID", districtID);

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);

                if (ja.Count > 0)
                {
                    JArray jaDiningHallTitleArray = new JArray();
                    JArray jaDiningHallArray = new JArray();
                    JObject joDiningHallItem = new JObject();
                    for (int diningHallIndex = 0; diningHallIndex < ja.Count; diningHallIndex++)
                    {
                        jaDiningHallTitleArray.Add((string)ja[diningHallIndex]["name"]);

                        joDiningHallItem = new JObject();
                        joDiningHallItem = (JObject)ja[diningHallIndex];
                        jaDiningHallArray.Add(joDiningHallItem);
                    }

                    joResult.Add("diningHallTitleArray", jaDiningHallTitleArray);
                    joResult.Add("diningHallArray", jaDiningHallArray);
                    joResult.Add("message", "食堂信息加载成功。");
                    joResult.Add("state", true);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "食堂信息加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取食堂信息(可选参数：食堂ID，区域ID，食堂编码，食堂名称)
        /// </summary>
        /// <param name="diningHall"></param>
        /// <param name="totalNumber"></param>
        /// <param name="totalPage"></param>
        /// <returns></returns>
        public List<System_DiningHall> GetDiningHallInfo(System_DiningHall diningHall, ref int totalNumber, ref int totalPage)
        {
            string dbName = GetDBName((int)diningHall.DistrictID);
            Manage_District mDistrict = new Manage_District();
            mDistrict.DistrictID = diningHall.DistrictID;
            var district = GetDistrictInfo(mDistrict);
            GBT.SystemBusinessCore.DbContext<DiningHall> dbContext = new GBT.SystemBusinessCore.DbContext<DiningHall>(dbName);
            var db = dbContext.Db;
            var diningHallInfoList = db.Queryable<DiningHall>()
                .WhereIF(diningHall.HallId != null, t => t.HallId == diningHall.HallId)
                .WhereIF(diningHall.DistrictID != null, t => t.DistrictID == diningHall.DistrictID)
                .WhereIF(!string.IsNullOrEmpty(diningHall.HallNumber), t => t.HallNumber.Contains(diningHall.HallNumber))
                .WhereIF(!string.IsNullOrEmpty(diningHall.HallName), t => t.HallName.Contains(diningHall.HallName))
                .Select(t => new System_DiningHall
                {
                    HallId = t.HallId,
                    HallNumber = t.HallNumber,
                    HallName = t.HallName,
                    Deadline = t.Deadline,
                    IsEnable = t.IsEnable,
                    DistrictID = t.DistrictID,
                    CreationDate = t.CreationDate
                }).ToPageList(diningHall.PageIndex, diningHall.PageSize, ref totalNumber, ref totalPage);

            var diningHallInfoListNew = diningHallInfoList
                .Select(t => new System_DiningHall
                {
                    HallId = t.HallId,
                    HallNumber = t.HallNumber,
                    HallName = t.HallName,
                    Deadline = t.Deadline,
                    IsEnable = t.IsEnable,
                    DistrictID = t.DistrictID,
                    DistrictNumber = district.DistrictNumber,
                    DistrictName = district.DistrictName,
                    CreationDate = t.CreationDate
                }).ToList();

            return diningHallInfoListNew;
        }

        /// <summary>
        /// 手机号码是否存在
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public bool IsMobile(string mobile, string dbName)
        {
            try
            {
                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "SELECT id,name,mobile FROM AddressBook WHERE mobile = @mobile "
                };

                comm.Parameters.AddWithValue("@mobile", mobile);

                JArray ja = qd.GetData(comm, dbName);
                if (ja.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        /// <summary>
        /// 工号是否存在
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public bool IsNumber(int districtID, string Number, SqlSugarClient db)
        {
            try
            {
                var userList = db.Queryable<AddressBook>().Where(t => t.DistrictID == districtID && t.Number == Number).ToList();
                if (userList.Count > 0)
                {
                    return true;
                }

                return false;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                throw;
            }
        }

        /// <summary>
        /// 获取通讯录信息
        /// </summary>
        /// <returns></returns>
        public JObject GetAddressBook(int CurrentPage, int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select id,name,mobile,districtID from AddressBook " +
                    "where districtID = @districtID and mobile not like '9%' order by id "
                };
                comm.Parameters.AddWithValue("@districtID", districtID);

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);

                if (ja.Count > 0)
                {
                    joResult = dp.PagingProcessing(CurrentPage, ja);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "通讯录加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取通讯录信息(可选参数：通讯录ID，区域ID，通讯录编码，通讯录名称)
        /// </summary>
        /// <param name="addressBook"></param>
        /// <param name="totalNumber"></param>
        /// <param name="totalPage"></param>
        /// <returns></returns>
        public List<System_AddressBook> GetAddressBookInfo(System_AddressBook addressBook, ref int totalNumber, ref int totalPage)
        {
            string dbName = GetDBName((int)addressBook.DistrictID);
            Manage_District mDistrict = new Manage_District();
            mDistrict.DistrictID = addressBook.DistrictID;
            var district = GetDistrictInfo(mDistrict);
            GBT.SystemBusinessCore.DbContext<AddressBook> dbContext = new GBT.SystemBusinessCore.DbContext<AddressBook>(dbName);
            var db = dbContext.Db;
            var addressBookList = db.Queryable<AddressBook>()
                .WhereIF(addressBook.ID != null, t => t.ID == addressBook.ID)
                .WhereIF(addressBook.DistrictID != null, t => t.DistrictID == addressBook.DistrictID)
                .WhereIF(!string.IsNullOrEmpty(addressBook.Number), t => t.Number.Contains(addressBook.Number))
                .WhereIF(!string.IsNullOrEmpty(addressBook.name), t => t.name.Contains(addressBook.name))
                .Select(t => new System_AddressBook
                {
                    ID = t.ID,
                    Number = t.Number,
                    name = t.name,
                    mobile = t.mobile,
                    DistrictID = t.DistrictID,
                    CreationDate = t.CreationDate
                }).ToPageList(addressBook.PageIndex, addressBook.PageSize, ref totalNumber, ref totalPage);

            var addressBookListNew = addressBookList
                .Select(t => new System_AddressBook
                {
                    ID = t.ID,
                    Number = t.Number,
                    name = t.name,
                    mobile = t.mobile,
                    DistrictID = t.DistrictID,
                    DistrictNumber = district.DistrictNumber,
                    DistrictName = district.DistrictName,
                    CreationDate = t.CreationDate
                }).ToList();

            return addressBookListNew;
        }

        /// <summary>
        /// 获取评价项目
        /// </summary>
        /// <returns></returns>
        public JObject GetEvaluation(int CurrentPage, int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select id,name,number,districtID,remark,isEnable from EvaluationContent " +
                    "where districtID = @districtID order by id "
                };
                comm.Parameters.AddWithValue("@districtID", districtID);

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);

                if (ja.Count > 0)
                {
                    joResult = dp.PagingProcessing(CurrentPage, ja);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "评价项目加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取评价项目信息(可选参数：评价项目ID，区域ID，评价项目编码，评价项目名称)
        /// </summary>
        /// <param name="evaluationContent"></param>
        /// <param name="totalNumber"></param>
        /// <param name="totalPage"></param>
        /// <returns></returns>
        public List<System_EvaluationContent> GetEvaluationInfo(System_EvaluationContent evaluationContent, ref int totalNumber, ref int totalPage)
        {
            string dbName = GetDBName((int)evaluationContent.DistrictID);
            Manage_District mDistrict = new Manage_District();
            mDistrict.DistrictID = evaluationContent.DistrictID;
            var district = GetDistrictInfo(mDistrict);
            GBT.SystemBusinessCore.DbContext<EvaluationContent> dbContext = new GBT.SystemBusinessCore.DbContext<EvaluationContent>(dbName);
            var db = dbContext.Db;
            var evaluationContentList = db.Queryable<EvaluationContent>()
                .WhereIF(evaluationContent.ID != null, t => t.ID == evaluationContent.ID)
                .WhereIF(evaluationContent.DistrictID != null, t => t.DistrictID == evaluationContent.DistrictID)
                .WhereIF(!string.IsNullOrEmpty(evaluationContent.Number), t => t.Number.Contains(evaluationContent.Number))
                .WhereIF(!string.IsNullOrEmpty(evaluationContent.Name), t => t.Name.Contains(evaluationContent.Name))
                .Select(t => new System_EvaluationContent
                {
                    ID = t.ID,
                    Number = t.Number,
                    Name = t.Name,
                    isEnable = t.isEnable,
                    DistrictID = t.DistrictID,
                }).ToPageList(evaluationContent.PageIndex, evaluationContent.PageSize, ref totalNumber, ref totalPage);

            var evaluationContentListNew = evaluationContentList
                .Select(t => new System_EvaluationContent
                {
                    ID = t.ID,
                    Number = t.Number,
                    Name = t.Name,
                    isEnable = t.isEnable,
                    DistrictID = t.DistrictID,
                    DistrictNumber = district.DistrictNumber,
                    DistrictName = district.DistrictName,
                }).ToList();

            return evaluationContentListNew;
        }

        /// <summary>
        /// 获取区域信息
        /// </summary>
        /// <returns></returns>
        public JObject GetDistrict(int CurrentPage, string name)
        {
            JObject joResult = new JObject();
            try
            {
                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select District.DistrictID AS id, District.DistrictNumber AS number, District.DistrictName AS name ," +
                    "ic.code, District.companyID,c.Number as companyNumber,c.Name as companyName,District.CreationDate from District " +
                    "inner join InvitationCode ic on District.DistrictID = ic.DistrictID " +
                    "inner join Company c on District.CompanyID = c.ID " +
                    "WHERE District.DistrictName like @name " +
                    "order by c.ID, District.DistrictID "
                };
                comm.Parameters.AddWithValue("@name", "%" + name + "%");
                JArray ja = qd.GetData(comm, "MealManage");

                if (ja.Count > 0)
                {
                    joResult = dp.PagingProcessing(CurrentPage, ja);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "区域加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取区域信息（当前公司）
        /// </summary>
        /// <returns></returns>
        public JObject GetDistrictCurrent(int companyID)
        {
            JObject joResult = new JObject();
            try
            {
                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select District.DistrictID AS id, District.DistrictNumber AS number, District.DistrictName AS name ," +
                    "ic.code, District.companyID, c.Number as companyNumber, c.Name as companyName,District.CreationDate from District " +
                    "inner join InvitationCode ic on District.DistrictID = ic.DistrictID " +
                    "inner join Company c on District.CompanyID = c.ID " +
                    "WHERE District.companyID = @companyID " +
                    "order by c.ID, District.DistrictID "
                };
                comm.Parameters.AddWithValue("@companyID", companyID);
                JArray ja = qd.GetData(comm, "MealManage");

                if (ja.Count > 0)
                {
                    joResult["data"] = ja;
                    joResult.Add("message", "区域加载成功。");
                    joResult.Add("state", true);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "区域加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取区域信息（当前公司）
        /// </summary>
        /// <returns></returns>
        public JObject GetDistrictCurrent(int CurrentPage, int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                int companyID = GetCompanyID(districtID);
                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select District.DistrictID AS id, District.DistrictNumber AS number, District.DistrictName AS name ," +
                    "ic.code, District.companyID,c.Number as companyNumber,c.Name as companyName,District.CreationDate from District " +
                    "inner join InvitationCode ic on District.DistrictID = ic.DistrictID " +
                    "inner join Company c on District.CompanyID = c.ID " +
                    "WHERE District.companyID = @companyID " +
                    "order by c.ID, District.DistrictID "
                };
                comm.Parameters.AddWithValue("@companyID", companyID);
                JArray ja = qd.GetData(comm, "MealManage");

                if (ja.Count > 0)
                {
                    joResult = dp.PagingProcessing(CurrentPage, ja);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "区域加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取区域列表（可选条件：公司ID，区域ID，区域编码，区域名称, 公司编码，公司名称）
        /// </summary>
        /// <param name="district"></param>
        /// <param name="totalNumber"></param>
        /// <param name="totalPage"></param>
        /// <returns></returns>
        public List<Manage_District> GetDistrictList(Manage_District district, ref int totalNumber, ref int totalPage)
        {
            GBT.ManageBusinessCore.DbContext<GBT.ManageEnties.District> dbContext = new GBT.ManageBusinessCore.DbContext<GBT.ManageEnties.District>();
            var db = dbContext.Db;
            var districtInfoList = db.Queryable<GBT.ManageEnties.District, GBT.ManageEnties.Company, GBT.ManageEnties.InvitationCode>((d, c, i) => new object[] {
                JoinType.Left, d.CompanyID == c.ID,
                JoinType.Left, d.DistrictID == i.DistrictID
            })
            .WhereIF(district.CompanyID != null, (d, c, i) => c.ID == district.CompanyID)
            .WhereIF(district.DistrictID != null, (d, c, i) => d.DistrictID == district.DistrictID)
            .WhereIF(!string.IsNullOrEmpty(district.DistrictNumber), (d, c, i) => d.DistrictNumber.Contains(district.DistrictNumber))
            .WhereIF(!string.IsNullOrEmpty(district.DistrictName), (d, c, i) => d.DistrictName.Contains(district.DistrictName))
            .WhereIF(!string.IsNullOrEmpty(district.CompanyName), (d, c, i) => c.Name.Contains(district.CompanyName))
            .WhereIF(!string.IsNullOrEmpty(district.CompanyNumber), (d, c, i) => c.Number.Contains(district.CompanyNumber))
            .OrderBy((d, c, i) => d.CreationDate, OrderByType.Desc)
            .Select((d, c, i) => new Manage_District
            {
                DistrictID = d.DistrictID,
                DistrictNumber = d.DistrictNumber,
                DistrictName = d.DistrictName,
                CompanyID = d.CompanyID,
                CompanyNumber = c.Number,
                CompanyName = c.Name,
                Code = i.code,
                CreationTime = d.CreationDate,
            }).ToPageList(district.PageIndex, district.PageSize, ref totalNumber, ref totalPage);
            return districtInfoList;
        }

        /// <summary>
        /// 获取公司信息
        /// </summary>
        /// <returns></returns>
        public JObject GetCompany(int CurrentPage, string name)
        {
            JObject joResult = new JObject();
            try
            {
                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "SELECT c.id,c.number,c.name,c.address,c.mobile,c.tel,c.legal_representative,c.reg_num,c.isAudit,c.remark,c.CreationTime, " +
                    "c.VersionID, v.Number as 'VersionNumber', v.Name as 'VersionName' FROM Company c " +
                    "LEFT JOIN Versions v ON c.VersionID = v.ID " +
                    "WHERE c.name like @name "
                };
                comm.Parameters.AddWithValue("@name", "%" + name + "%");
                JArray ja = qd.GetData(comm, "MealManage");

                if (ja.Count > 0)
                {
                    joResult = dp.PagingProcessing(CurrentPage, ja);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "公司信息加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取公司列表（可选参数：公司ID，版本ID，公司编码，公司名称，版本编码，版本名称）
        /// </summary>
        /// <param name="manage_Company"></param>
        /// <param name="totalNumber"></param>
        /// <param name="totalPage"></param>
        /// <returns></returns>
        public List<Manage_Company> GetCompanyList(Manage_Company manage_Company, ref int totalNumber, ref int totalPage)
        {
            GBT.ManageBusinessCore.DbContext<GBT.ManageEnties.Company> dbContext = new GBT.ManageBusinessCore.DbContext<GBT.ManageEnties.Company>();
            var db = dbContext.Db;
            var companyInfoList = db.Queryable<GBT.ManageEnties.Company, GBT.ManageEnties.Versions>((c, v) => new object[] {
                JoinType.Left, c.VersionID == v.ID
            })
            .WhereIF(manage_Company.ID != null, (c, v) => c.ID == manage_Company.ID)
            .WhereIF(manage_Company.VersionID != null, (c, v) => c.VersionID == manage_Company.VersionID)
            .WhereIF(!string.IsNullOrEmpty(manage_Company.Number), (c, v) => c.Number.Contains(manage_Company.Number))
            .WhereIF(!string.IsNullOrEmpty(manage_Company.Name), (c, v) => c.Name.Contains(manage_Company.Name))
            .WhereIF(!string.IsNullOrEmpty(manage_Company.VersionNumber), (c, v) => v.Number.Contains(manage_Company.VersionNumber))
            .WhereIF(!string.IsNullOrEmpty(manage_Company.VersionName), (c, v) => v.Name.Contains(manage_Company.VersionName))
            .OrderBy((c, v) => c.CreationTime, OrderByType.Desc)
            .Select((c, v) => new Manage_Company
            {
                ID = c.ID,
                VersionID = c.VersionID,
                Number = c.Number,
                Name = c.Name,
                VersionNumber = v.Number,
                VersionName = v.Name,
                ExpirationDate = c.ExpirationDate,
                Remark = c.Remark,
                CreationTime = c.CreationTime
            }).ToPageList(manage_Company.PageIndex, manage_Company.PageSize, ref totalNumber, ref totalPage);

            return companyInfoList;
        }

        /// <summary>
        /// 获取菜品分类信息
        /// </summary>
        /// <returns></returns>
        public JObject GetDishesClass(int CurrentPage, int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select dc.id,dc.number,dc.name,dc.districtID,dc.hallID, dh.hallNumber, dh.hallName,dc.remark,dc.qty from DishesClass dc " +
                    "inner join DiningHall dh on dc.hallID = dh.hallID " +
                    "where dc.districtID = @districtID order by dc.number "
                };
                comm.Parameters.AddWithValue("@districtID", districtID);

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);

                if (ja.Count > 0)
                {
                    joResult = dp.PagingProcessing(CurrentPage, ja);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "菜品分类加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取菜品分类信息(可选参数：菜品分类ID，区域ID，菜品分类编码，菜品分类名称)
        /// </summary>
        /// <param name="dishesClass"></param>
        /// <param name="totalNumber"></param>
        /// <param name="totalPage"></param>
        /// <returns></returns>
        public List<System_DishesClass> GetDishesClassInfo(System_DishesClass dishesClass, ref int totalNumber, ref int totalPage)
        {
            string dbName = GetDBName((int)dishesClass.DistrictID);
            Manage_District mDistrict = new Manage_District();
            mDistrict.DistrictID = dishesClass.DistrictID;
            var district = GetDistrictInfo(mDistrict);
            GBT.SystemBusinessCore.DbContext<GBT.SystemEnties.DishesClass> dbContext = new GBT.SystemBusinessCore.DbContext<GBT.SystemEnties.DishesClass>(dbName);
            var db = dbContext.Db;
            var dishesClassList = db.Queryable<GBT.SystemEnties.DishesClass, DiningHall>((a, d) => new object[] {
                    JoinType.Left, a.HallID == d.HallId
                })
                .WhereIF(dishesClass.ID != null, (a, d) => a.ID == dishesClass.ID)
                .WhereIF(dishesClass.HallID != null, (a, d) => a.HallID == dishesClass.HallID)
                .WhereIF(dishesClass.DistrictID != null, (a, d) => a.DistrictID == dishesClass.DistrictID)
                .WhereIF(!string.IsNullOrEmpty(dishesClass.Number), (a, d) => a.Number.Contains(dishesClass.Number))
                .WhereIF(!string.IsNullOrEmpty(dishesClass.Name), (a, d) => a.Name.Contains(dishesClass.Name))
                .Select((a, d) => new System_DishesClass
                {
                    ID = a.ID,
                    HallID = a.HallID,
                    Number = a.Number,
                    Name = a.Name,
                    qty = a.qty,
                    HallNumber = d.HallNumber,
                    HallName = d.HallName,
                    DistrictID = a.DistrictID,
                }).ToPageList(dishesClass.PageIndex, dishesClass.PageSize, ref totalNumber, ref totalPage);

            var dishesClassListNew = dishesClassList
                .Select(t => new System_DishesClass
                {
                    ID = t.ID,
                    HallID = t.HallID,
                    Number = t.Number,
                    Name = t.Name,
                    qty = t.qty,
                    HallNumber = t.HallNumber,
                    HallName = t.HallName,
                    DistrictID = t.DistrictID,
                    DistrictNumber = district.DistrictNumber,
                    DistrictName = district.DistrictName,
                }).ToList();

            return dishesClassListNew;
        }

        /// <summary>
        /// 获取菜品分类信息(picker)
        /// </summary>
        /// <returns></returns>
        public JObject GetDishesClassPicker(int hallID, int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select dc.id,dc.number,dc.name,dc.districtID,dc.hallID, dh.hallNumber, dh.hallName,dc.remark,dc.qty from DishesClass dc " +
                    "inner join DiningHall dh on dc.hallID = dh.hallID " +
                    "where dc.districtID = @districtID and dc.hallID = @hallID order by dc.number "
                };
                comm.Parameters.AddWithValue("@districtID", districtID);
                comm.Parameters.AddWithValue("@hallID", hallID);

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);

                if (ja.Count > 0)
                {
                    joResult.Add("data", ja);
                    joResult.Add("message", "菜品分类加载成功。");
                    joResult.Add("state", true);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "菜品分类加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取属性信息（picker）
        /// </summary>
        /// <returns></returns>
        public JObject GetAttributePicker(int hallID, int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";
                dbName = GetDBName(districtID);

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select a.id,a.number,a.name,a.type,a.districtID,a.hallID,dh.hallNumber,dh.hallName,a.remark from Attribute a " +
                    "inner join DiningHall dh on dh.hallID = a.hallID " +
                    "where a.hallID = @hallID and a.districtID = @districtID order by a.hallID "
                };
                comm.Parameters.AddWithValue("@hallID", hallID);
                comm.Parameters.AddWithValue("@districtID", districtID);

                JArray ja = qd.GetData(comm, dbName);

                if (ja.Count > 0)
                {
                    JArray jaArributeTitleArray = new JArray();
                    JArray jaArributeArray = new JArray();
                    JObject joArributeItem = new JObject();
                    for (int arributeIndex = 0; arributeIndex < ja.Count; arributeIndex++)
                    {
                        jaArributeTitleArray.Add((string)ja[arributeIndex]["name"]);

                        joArributeItem = new JObject();
                        joArributeItem = (JObject)ja[arributeIndex];
                        jaArributeArray.Add(joArributeItem);
                    }

                    joResult.Add("arributeTitleArray", jaArributeTitleArray);
                    joResult.Add("arributeArray", jaArributeArray);
                    joResult.Add("message", "属性信息加载成功。");
                    joResult.Add("state", true);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "属性信息加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取属性信息
        /// </summary>
        /// <returns></returns>
        public JObject GetAttribute(int CurrentPage, int districtID)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select a.id,a.number,a.name,a.type,a.districtID,a.hallID,dh.hallNumber,dh.hallName,a.remark from Attribute a " +
                    "inner join DiningHall dh on dh.hallID = a.hallID " +
                    "where a.districtID = @districtID order by a.hallID "
                };
                comm.Parameters.AddWithValue("@districtID", districtID);

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);

                if (ja.Count > 0)
                {
                    joResult = dp.PagingProcessing(CurrentPage, ja);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "属性加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取属性信息(可选参数：属性ID，区域ID，属性编码，属性名称)
        /// </summary>
        /// <param name="attribute"></param>
        /// <param name="totalNumber"></param>
        /// <param name="totalPage"></param>
        /// <returns></returns>
        public List<System_Attribute> GetAttributeInfo(System_Attribute attribute, ref int totalNumber, ref int totalPage)
        {
            string dbName = GetDBName((int)attribute.DistrictID);
            Manage_District mDistrict = new Manage_District();
            mDistrict.DistrictID = attribute.DistrictID;
            var district = GetDistrictInfo(mDistrict);
            GBT.SystemBusinessCore.DbContext<GBT.SystemEnties.Attribute> dbContext = new GBT.SystemBusinessCore.DbContext<GBT.SystemEnties.Attribute>(dbName);
            var db = dbContext.Db;
            var attributeList = db.Queryable<GBT.SystemEnties.Attribute, DiningHall>((a, d) => new object[] {
                    JoinType.Left, a.HallID == d.HallId
                })
                .WhereIF(attribute.ID != null, (a, d) => a.ID == attribute.ID)
                .WhereIF(attribute.HallID != null, (a, d) => a.HallID == attribute.HallID)
                .WhereIF(attribute.DistrictID != null, (a, d) => a.DistrictID == attribute.DistrictID)
                .WhereIF(!string.IsNullOrEmpty(attribute.Number), (a, d) => a.Number.Contains(attribute.Number))
                .WhereIF(!string.IsNullOrEmpty(attribute.Name), (a, d) => a.Name.Contains(attribute.Name))
                .Select((a, d) => new System_Attribute
                {
                    ID = a.ID,
                    HallID = a.HallID,
                    Type = a.Type,
                    Number = a.Number,
                    Name = a.Name,
                    HallNumber = d.HallNumber,
                    HallName = d.HallName,
                    DistrictID = a.DistrictID,
                }).ToPageList(attribute.PageIndex, attribute.PageSize, ref totalNumber, ref totalPage);

            var attributeListNew = attributeList
                .Select(t => new System_Attribute
                {
                    ID = t.ID,
                    HallID = t.HallID,
                    Type = t.Type,
                    TypeName = t.Type == 0 ? "菜品属性" : "待定义",
                    Number = t.Number,
                    Name = t.Name,
                    HallNumber = t.HallNumber,
                    HallName = t.HallName,
                    DistrictID = t.DistrictID,
                    DistrictNumber = district.DistrictNumber,
                    DistrictName = district.DistrictName,
                }).ToList();

            return attributeListNew;
        }

        /// <summary>
        /// 获取菜品信息(菜品设置使用)
        /// </summary>
        /// <returns></returns>
        public JObject GetDishesSet(JObject joParam)
        {
            JObject joResult = new JObject();
            try
            {
                int CurrentPage = (int)joParam["currentPage"];
                int districtID = (int)joParam["districtID"];
                int hallID = (int)joParam["hallID"];
                string attributeID = (string)joParam["attributeID"];
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select d.id,d.dishesClassID,d.number,d.name,d.imgUrl,d.remark,d.isEnable,dh.hallId,dh.hallNumber,dh.hallName,dh.deadLine, " +
                    "dc.Number as dishesClassNumber,dc.name as dishesClassName,dc.qty,da.name as dishesAttributeName,a.number as attributeNumber, " +
                    "a.name as attributeName from Dishes d inner join DiningHall dh on d.HallID = dh.HallId and d.districtID = dh.DistrictID " +
                    "inner join DishesClass dc on d.DishesClassID = dc.ID and d.districtID = dc.districtID and d.hallID = dc.hallID " +
                    "left join DishesAttribute da on da.DishesID = d.ID and d.districtID = da.districtID and d.hallID = da.hallID " +
                    "left join Attribute a on da.AttributeID = a.ID and d.districtID = a.districtID and d.hallID = a.hallID and a.[type] = 0 " +
                    "where d.districtID = @districtID and d.hallID = @hallID and ISNULL(a.id,'') like @attributeID " +
                    "order by dc.number,d.id,a.id "
                };
                comm.Parameters.AddWithValue("@districtID", districtID);
                comm.Parameters.AddWithValue("@hallID", hallID);
                comm.Parameters.AddWithValue("@attributeID", "%" + attributeID + "%");

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);
                JObject item = new JObject();
                JArray jaDishes = new JArray();
                JObject joDishes = new JObject();
                int dishesID = 0;
                int newDishesIndex = 0;
                string attributeName = "";

                if (ja.Count > 0)
                {
                    for (int dishesIndex = 0; dishesIndex < ja.Count; dishesIndex++)
                    {
                        item = (JObject)ja[dishesIndex];
                        joDishes = new JObject();
                        if (dishesID != (int)item["id"])
                        {
                            newDishesIndex++;
                            attributeName = (string)item["attributeName"] + " ";
                            joDishes.Add("id", (int)item["id"]);
                            joDishes.Add("number", (string)item["number"]);
                            joDishes.Add("name", (string)item["name"]);
                            joDishes.Add("dishesClassID", (int)item["dishesClassID"]);
                            joDishes.Add("dishesClassName", (string)item["dishesClassName"]);
                            joDishes.Add("imgUrl", (string)item["imgUrl"]);
                            joDishes.Add("attributeName", attributeName.Trim());
                            joDishes.Add("isEnable", (bool)item["isEnable"]);
                            jaDishes.Add(joDishes);
                            dishesID = (int)item["id"];
                        }
                        else
                        {
                            attributeName += (string)item["attributeName"] + " ";
                            jaDishes[newDishesIndex - 1]["attributeName"] = attributeName.Trim();
                        }
                    }

                    joResult = dp.PagingProcessing(CurrentPage, jaDishes);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "菜品加载失败。");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取菜品信息 详情页
        /// </summary>
        /// <returns></returns>
        public JObject GetDishesDetail(int districtID, int id)
        {
            JObject joResult = new JObject();
            try
            {
                string dbName = "";

                SqlCommand comm = new SqlCommand()
                {
                    CommandText = "select d.id,d.dishesClassID,d.number,d.name,d.imgUrl,d.remark,d.districtID,d.isEnable,dh.hallID,dh.hallNumber,dh.hallName,dh.deadLine, " +
                    "dc.Number as dishesClassNumber,dc.name as dishesClassName,dc.qty,ISNULL(da.name,'') as dishesAttributeName," +
                    "ISNULL(a.id,0) as attributeID,ISNULL(a.number,'') as attributeNumber, " +
                    "ISNULL(a.name,'') as attributeName,ISNULL(da.remark,'') as dishesAttributeRemark,ISNULL(a.[type],0) as [type],ISNULL(da.id,0) as dishesAttributeID " +
                    "from Dishes d inner join DiningHall dh on d.HallID = dh.HallId and d.districtID = dh.DistrictID " +
                    "inner join DishesClass dc on d.DishesClassID = dc.ID and d.districtID = dc.districtID and d.hallID = dc.hallID " +
                    "left join DishesAttribute da on da.DishesID = d.ID and d.districtID = da.districtID and d.hallID = da.hallID " +
                    "left join Attribute a on da.AttributeID = a.ID and d.districtID = a.districtID and d.hallID = a.hallID and a.[type] = 0 " +
                    "where d.id = @id and d.districtID = @districtID " +
                    "order by dc.id,a.id  "
                };
                comm.Parameters.AddWithValue("@id", id);
                comm.Parameters.AddWithValue("@districtID", districtID);

                dbName = GetDBName(districtID);

                JArray ja = qd.GetData(comm, dbName);
                JObject item = new JObject();
                JObject joDishes = new JObject();
                JObject joAttribute = new JObject();
                JArray jaAttribute = new JArray();
                string attributeName = "";
                int dishesID = 0;

                if (ja.Count > 0)
                {
                    for (int index = 0; index < ja.Count; index++)
                    {
                        item = (JObject)ja[index];
                        if ((int)item["dishesAttributeID"] != 0)
                        {
                            joAttribute = new JObject();
                            joAttribute["id"] = (int)item["dishesAttributeID"];//菜品属性ID
                            joAttribute["attributeID"] = (int)item["attributeID"];//属性ID 
                            joAttribute["hallID"] = (int)item["hallID"];
                            joAttribute["number"] = (string)item["attributeNumber"];
                            joAttribute["name"] = (string)item["attributeName"];
                            joAttribute["type"] = (int)item["type"];
                            joAttribute["remark"] = (string)item["dishesAttributeRemark"];
                            joAttribute["districtID"] = (int)item["districtID"];
                            jaAttribute.Add(joAttribute);
                        }
                        if (dishesID != (int)item["id"])
                        {
                            attributeName = (string)item["attributeName"] + " ";
                            joDishes["id"] = (int)item["id"];
                            joDishes["number"] = (string)item["number"];
                            joDishes["name"] = (string)item["name"];
                            joDishes["imgUrl"] = (string)item["imgUrl"];
                            joDishes["districtID"] = (int)item["districtID"];
                            joDishes["dishesClassID"] = (int)item["dishesClassID"];
                            joDishes["dishesClassNumber"] = (string)item["dishesClassNumber"];
                            joDishes["dishesClassName"] = (string)item["dishesClassName"];
                            joDishes["hallID"] = (int)item["hallID"];
                            joDishes["hallNumber"] = (string)item["hallNumber"];
                            joDishes["hallName"] = (string)item["hallName"];
                            joDishes["remark"] = (string)item["remark"];
                            joDishes["isEnable"] = (bool)item["isEnable"];
                            joDishes["attributeNameGroup"] = attributeName.Trim();
                            dishesID = (int)item["id"];
                        }
                        else
                        {
                            if ((int)item["dishesAttributeID"] != 0)
                            {
                                attributeName += (string)item["attributeName"] + " ";
                                joDishes["attributeNameGroup"] = attributeName.Trim();
                            }
                        }
                    }
                    joDishes["attributeList"] = jaAttribute;

                    joResult.Add("data", joDishes);
                    joResult.Add("message", "菜品加载成功");
                    joResult.Add("state", true);
                }
                else
                {
                    joResult.Add("data", new JArray());
                    joResult.Add("message", "菜品加载失败");
                    joResult.Add("state", false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
                joResult.Add("data", new JArray());
                joResult.Add("message", ex.Message + "\r\n" + ex.StackTrace);
                joResult.Add("state", false);
            }
            return joResult;
        }

        /// <summary>
        /// 获取版本信息
        /// </summary>
        /// <returns></returns>
        public List<GBT.ManageEnties.Versions> GetVersionInfo()
        {
            List<GBT.ManageEnties.Versions> versionList = new List<GBT.ManageEnties.Versions>();
            try
            {
                GBT.ManageBusinessCore.DbContext<GBT.ManageEnties.Versions> dbContext = new GBT.ManageBusinessCore.DbContext<GBT.ManageEnties.Versions>();
                var db = dbContext.Db;
                versionList = dbContext.GetList();
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            return versionList;
        }

        /// <summary>
        /// 获取食堂名称
        /// </summary>
        /// <param name="hallID"></param>
        /// <param name="dbName"></param>
        /// <returns></returns>
        public string GetHallName(int hallID, string dbName)
        {
            DbContext<DiningHall> dbContext = new DbContext<DiningHall>(dbName);
            var diningHall = dbContext.GetList(t => t.HallId == hallID).FirstOrDefault();
            return diningHall.HallName;
        }

        /// <summary>
        /// 获取餐点信息
        /// </summary>
        /// <param name="dbName"></param>
        /// <returns></returns>
        public List<MealTime> GetMealTime(string dbName)
        {
            DbContext<MealTime> dbContext = new DbContext<MealTime>(dbName);
            var mealTime = dbContext.GetList();
            return mealTime;
        }

        /// <summary>
        /// 获取用户余额信息, 不存在则新增账户余额记录
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="dbName"></param>
        /// <returns></returns>
        public UserStoredValue GetUserStoredValue(int userID, string dbName)
        {
            DbContext<UserStoredValue> dbContext = new DbContext<UserStoredValue>(dbName);
            var userStoredValue = dbContext.GetList(t => t.UserID == userID)?.FirstOrDefault();
            if (userStoredValue == null)
            {
                userStoredValue = new UserStoredValue();
                userStoredValue.UserID = userID;
                userStoredValue.Value = 0;
                userStoredValue.CreationDate = DateTime.Now;
                userStoredValue = dbContext.InsertReturnEntity(userStoredValue);
            }
            return userStoredValue;
        }

        /// <summary>
        /// 获取版本信息
        /// </summary>
        /// <param name="districtID"></param>
        /// <returns></returns>
        public GBT.ManageEnties.Versions GetVersions(int districtID)
        {
            GBT.ManageBusinessCore.DbContext<District> dbContext = new GBT.ManageBusinessCore.DbContext<District>();
            var db = dbContext.Db;
            var vresion = db.Queryable<District, Company, Versions>((d, c, v) => new object[]{
                JoinType.Left, d.CompanyID == c.ID,
                JoinType.Left, c.VersionID == v.ID,
            })
            .Where((d, c, v) => d.DistrictID == districtID)
            .Select((d, c, v) => v)
            .First();
            return vresion;
        }
    }
}
